package cn.bdqn.service.impl;

import cn.bdqn.domain.Cargo;
import cn.bdqn.mapper.CargoMapper;
import cn.bdqn.service.CargoService;
import cn.bdqn.utils.result.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 货物业务实现类
 */
@Service
public class CargoServiceImpl implements CargoService {

    @Autowired
    private CargoMapper cargoMapper;

    /**
     * 根据主键id查询
     * @param id
     * @return
     */
    @Override
    public Cargo queryByPrimaryKey(Integer id) {
        return cargoMapper.selectByPrimaryKey(id);
    }

    /**
     * 多条件查询   分页
     * @param cargo
     * @return
     */
    @Override
    public Page queryByCargo(Cargo cargo,Integer pageNum,Integer pageAmount){

        // 开始分页
        PageHelper.startPage(pageNum>0?pageNum:1,pageAmount);

        // 获取货物信息
        List<Cargo> cargoList = cargoMapper.selectByCargo(cargo);

        // 分页
        PageInfo<Cargo> cargoPageInfo = new PageInfo<>(cargoList);

        // 封装分页信息
        Page page = new Page();

        page.setMenuList(cargoPageInfo.getList());

        page.setTotalRecord((int) cargoPageInfo.getTotal());

        return page;
    }

    /**
     * 新增
     * @param cargo
     */
    @Override
    public void insert(Cargo cargo){
        cargoMapper.insert(cargo);
    }

    /**
     * 选择更新
     * @param cargo
     */
    @Override
    public void updateByPrimaryKeySelective(Cargo cargo){
        cargoMapper.updateByPrimaryKeySelective(cargo);
    }

}
